/**
 * 
 */
  $(document).ready(function() {
	  var options = { 
			    	 url:"/salonmorandsmigi/CourseServlet?action=add", 
			    	 beforeSubmit: validator,
			    	 success:function() { 
		    		 			$(".submit").append("<div class=\"success\">Added successfully!</div>");
		    		 			setTimeout("$(\".success\").remove();",2000);
		    		 			setTimeout("window.location.href =\"/salonmorandsmigi/courses.jsp\";",3000);
		    	 				},
				     statusCode: {
				    	 			400: function() {
					    	 			$(".submit").append("<div class=\"fail\">FAIL! bad session input! try again!<\div>");
					    	 			setTimeout("$(\".fail\").remove();",2000);
				    	 			},
			    	 				402: function() {
			    	 					$(".submit").append("<div class=\"fail\">FAIL! course collides with other courses!<\div>");
					    	 			setTimeout("$(\".fail\").remove();",2000);
			    	 				},
			    	 				405: function() {
			    	 					$(".submit").append("<div class=\"fail\">FAIL! course name already exists!<\div>");
					    	 			setTimeout("$(\".fail\").remove();",2000);
			    	 				},
			    	 				500: function() {
			    	 					$(".submit").append("<div class=\"fail\">FAIL! server error!<\div>");
					    	 			setTimeout("$(\".fail\").remove();",2000);
			    	 				}
	  							 }
			    	}; 
	  
	  $("#newCourse").validate({
			rules: {
				name: "required",
				description: {
					required: true,
					minlength: 20
				},
				credit: {
					required: true,
					range: [1, 6]
				},
				capacity: {
					required: true,
					min: 1
				},
				gnum:{
					required: true,
					min: 0
				}
			},
			messages: {
				name: "Please enter the course name",
				description: {
					required: "Please enter the course description",
					minlength: "Description must be at least 20 characters"
				},
				credit: {
					required: "Please ener the number of credit points",
					range: "credit can be beteween 1-6 points."
				},
				capacity: {
					required: "Please enter the capacity of students.",
					min: "The capacity must be at least 1 student."
				},
				gnum: {
					required: "Please enter the group number for the course.",
					min: "The group number must be non-negative."
				}
			},
			submitHandler: function(form) {
			   	$(form).ajaxSubmit(options);
			   }
		});
	  
	  function validator() {
		  return $("#newCourse").validate().form;
	  }
	  
  });
    
